package org.abacus.client.ui.view;

import java.awt.Toolkit;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.abacus.client.common.ExitHelper;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Scrollable;
import org.eclipse.swt.widgets.Shell;

import request.ActionRequest;


public abstract class View implements IShellProvider {
	private ActionRequest actionRequest;

	protected final Map<String, Scrollable> scrollables = new HashMap<String, Scrollable>();
	protected final static Display display = Display.getDefault();
	protected final Shell shell;

	public View(int style) {
		super();
		shell = new Shell(display, style);
		shell.addDisposeListener(ExitHelper.disposeListener);
		this.initView();
	}

	protected abstract void initView();

	public static Display getDisplay() {
		return display;
	}

	public Shell getShell() {
		return shell;
	}

	public Set<Entry<String, Scrollable>> getEntrySetScrollables() {
		return scrollables.entrySet();
	}

	public Scrollable setScrollable(String key, Scrollable value) {
		return scrollables.put(key, value);
	}

	public Scrollable getScrollable(String key) {
		return scrollables.get(key);
	}

	public final static void centerShell(Display display, Shell shell) {
		Rectangle displayBounds = display.getPrimaryMonitor().getBounds();
		Rectangle shellBounds = shell.getBounds();
		int x = displayBounds.x + (displayBounds.width - shellBounds.width) >> 1;
		int y = displayBounds.y + (displayBounds.height - shellBounds.height) >> 1;
		shell.setLocation(x, y);
	}

	public final static void centerShell(Shell shell) {
		int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
		int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
		int shellHeight = shell.getBounds().height;
		int shellWidth = shell.getBounds().width;
		if (shellHeight > screenHeight)
			shellHeight = screenHeight;
		if (shellWidth > screenWidth)
			shellWidth = screenWidth;
		shell.setLocation(((screenWidth - shellWidth) / 2), ((screenHeight - shellHeight) / 2));
	}

	public final static void centerShell(Shell shell, int width, int height) {
		int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
		int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
		shell.setMinimumSize(width, height);
		shell.setBounds((screenWidth - width) / 2, ((screenHeight - height) / 2), width, height);
	}

	public ActionRequest getActionRequest() {
		return actionRequest;
	}

	public void setActionRequest(ActionRequest actionRequest) {
		this.actionRequest = actionRequest;
	}

	public abstract void ok();

	public abstract void cancel();

	public abstract void dispose();

}
